﻿@model EntityField
@using NewLife;
@using NewLife.Cube
@using NewLife.Cube.ViewModels;
@using XCode;
@using XCode.Configuration;
@{
    var entity = Model.Entity;
    var item = Model.Field;

    var fields = ViewBag.Fields as FieldCollection;
    var dataField = fields.GetField(item.Name) as ListField;
    var dataSource = dataField?.DataSource;

    var err = 0;
    if (ViewData.ModelState.TryGetValue(item.Name, out var st) && st != null) { err = st.Errors.Count; }

    var des = item.Description.TrimStart(item.DisplayName).TrimStart(",", ".", "，", "。");
    var display = dataField?.DisplayName ?? item.DisplayName;
    var className = !item.Nullable&&!item.Name.EqualIgnoreCase("CreateUserID", "CreateUser", "CreateTime", "CreateIP",
                "UpdateUserID", "UpdateUser", "UpdateTime", "UpdateIP", "StateCode", "StatusCode","Auditor","AuditTime","EffectiveTime","DeactivateTime","BomDetailedIDs") ? "layui-form-required" : "";


    //var total = 12;
    //var label = 3;
    //var span = 4;
    //if (err == 0 && des.IsNullOrEmpty())
    //{
    //    span = 0;
    //}
    //else if (item.Type == typeof(Boolean) || item.Type.IsEnum)
    //{
    //    span += 1;
    //}
    //var input = total - label - span;
}
<!-- 字段标签（含帮助说明信息） -->
        <label class="layui-form-label @className">@display</label>
        <div class="layui-input-block">
    <!-- 字段编辑框 -->
		@if (dataSource != null)
		{
			@*@Html.ForListBox(item.Name, dataSource(entity, item), entity[item.Name] as String)*@
			@await Html.PartialAsync("_Form_Type_ListBox", new ListBoxModel(item.Name, dataSource(entity), entity[item.Name]))
		}
		else
		{
			var itemModel = new ItemModel(item.Name, entity[item.Name], item.Type) { Length = item.Length };

			// 系统字段默认不允许编辑,不显示
			if (item.Name.EqualIgnoreCase("CreateUserID", "CreateUser", "CreateTime", "CreateIP",
			"UpdateUserID", "UpdateUser", "UpdateTime", "UpdateIP", "StateCode", "StatusCode","Auditor","AuditTime","EffectiveTime","DeactivateTime","BomDetailedIDs"))
			{
				@*@Html.ForString(item.Name, (String)entity[item.Name], item.Length, new { @disabled = "disabled" }.ToDictionary())*@
				itemModel.HtmlAttributes = new { disabled = "disabled" };
				@*@await Html.PartialAsync("_Form_Type_String", itemModel)*@
                @await Html.PartialAsync("_Form_Type_String_DataField", new ValueTuple<ItemModel, DataField>(itemModel, item))
			}
			else
			{
				@*@Html.ForEditor(item, entity)*@
				if (!item.MapField.IsNullOrEmpty() && item.MapProvider != null)
				{
					@await Html.PartialAsync("_Form_Type_Map", Model)
				}
				else if (item.ReadOnly)
				{
                    @await Html.PartialAsync("_Form_Type_String_DataField", new ValueTuple<ItemModel, DataField>(itemModel, item))
					<label class="layui-form-label @className">@entity[item.Name]</label>
				}
				else if (item.Type == typeof(String))
				{
					if (item.ItemType.EqualIgnoreCase("file", "image"))
					{
						@await Html.PartialAsync("_Form_Type_File", Model)
					}
					else
					{
						@*@Html.ForString(item.Name, (String)entity[item.Name], item.Length)*@
						@await Html.PartialAsync("_Form_Type_String_DataField", new ValueTuple<ItemModel, DataField>(itemModel, item))
					}
				}
				else if (entity is IEntityTree treeEntity)
				{
					var set = entity.GetType().GetValue("Setting") as IEntityTreeSetting;
					if (set == null || set.Parent != item.Name)
					{
						@await Html.PartialAsync("_Form_Editor", new EntityField(entity, item))
					}
					else
					{
						var root = entity.GetType().GetValue("Root") as IEntityTree;
						// 找到完整菜单树，但是排除当前节点这个分支
						var list = root.FindAllChildsExcept(treeEntity);
						var data = new SelectList(list, set.Key, "TreeNodeText", entity[item.Name]);
						@Html.DropDownList(item.Name, data, new { @class = "multiselect" })
					}
				}
				else
				{
					@*@Html.ForEditor(item.Name, entity[item.Name], item.Type)*@
					@await Html.PartialAsync("_Form_Editor", new EntityField(entity, item))
				}
			}
		}

	@if (err > 0)
	{
		<div class="layui-form-mid layui-word-aux">
			@Html.ValidationMessage(item.Name)
		</div>
	}
        </div>

